package com.marui.ch2.tools;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class UserCyclicBarrier2 {

    private static CyclicBarrier barrier = new CyclicBarrier(3,new play());

    private static class work implements Runnable{
        @Override
        public void run() {
            try {
                Thread.sleep(100);
                System.out.println("working");
                barrier.await();
                System.out.println("finish");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    private static class work2 implements Runnable{
        @Override
        public void run() {
            try {
                Thread.sleep(100);
                System.out.println("working2");
                barrier.await();
                System.out.println("finish2");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    private static class play implements Runnable{
        @Override
        public void run() {
            System.out.println("playing");
        }
    }


    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        work work = new work();
        Thread t1 = new Thread(work);
        Thread t2 = new Thread(work);
        Thread t3 = new Thread(work);
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(4000);
        System.out.println("111111111111111111111111111111111");
        barrier.reset();
        work2 work2 = new work2();
        Thread t21 = new Thread(work2);
        Thread t22 = new Thread(work2);
        Thread t23 = new Thread(work2);
        t21.start();
        t22.start();
        t23.start();

    }
}
